
** Loading data - generated in dofile: data05
use "$data\minwage_data5_estimation_2012_2015.dta", clear
keep if inrange(tline,-24,24) & apprentice == 0 // 49 event-month oberservation window & excluding apprentices

* we use 2015 for employment fractions for hourly wages.
keep if indkaar == 2015 

** Generating setor variable using the first 4 digits from SKATs hirachical sector variable: es7606_hov_br_nr  
local x = 4
tostring es7606_hov_br_nr, replace force
ge es7606_sub`x' = substr(es7606_hov_br_nr,1, `x')
drop es7606_hov_br_nr
rename es7606_sub`x' es7606_hov_br_nr
destring es7606_hov_br_nr, replace

replace emplyd = emplyd * 100
replace felt_200_trim = 0 if felt_200_trim == . // if no reported earnings, earnings set to zero
replace timeantF12_trim = 0 if timeantF12_trim ==. // if no reported hours, hours set to zero
gen emonth = ym(cohort, brthmnth) 

 qui: tab tline, gen(Dtime) // 49 event months
 qui: tab month, gen(Dmnth) // 48 Calendar months
 qui: tab cohort, gen(Dcoh) // 8 birth cohorts
 qui: tab emonth, gen(Dbmonth) // 96 birth months
 ge tline_pos = tline+ 25

 egen cohmxmonth = group(tline month) // month cohorts 2352 

** Time dummies   
ge Dtime0 = tline>=0
ge DtimeZ = tline == 0

** Age polynomial
ge tline_pos_sq=tline_pos*tline_pos
ge tline_pos_cu=tline_pos*tline_pos*tline_pos
ge tline_pos_4th=tline_pos*tline_pos*tline_pos*tline_pos
ge tline_pos_5th=tline_pos*tline_pos*tline_pos*tline_pos*tline_pos

** Table A.4: Estimates for Supermarkets and Grocery Stores
** NB. Loop includes results for restarants and bakeries: the were dropped from the paper to meet the space limitations.
local x = 2
forval j = 1/3{
preserve
	
	if `j' == 1{
		ge emplyd_es = 0 
		replace emplyd_es = 100 if emplyd == 100 & (es7606_hov_br_nr == 4711 | es7606_hov_br_nr == 4719 ) // Retail (Supermarkets)
		ge calc_hrly_wage_es = .
		replace calc_hrly_wage_es = calc_hrly_wage if (es7606_hov_br_nr == 4711 | es7606_hov_br_nr == 4719 ) 
		local sector Retail
		local z 12
	}
	if `j' == 2{
		ge emplyd_es = 0 
		replace emplyd_es = 100 if emplyd == 100 & (es7606_hov_br_nr == 5610 ) // Restaurants 
		ge calc_hrly_wage_es = .
		replace calc_hrly_wage_es = calc_hrly_wage if (es7606_hov_br_nr == 5610) 
		local sector Restaurants
		local z 13
	}
	if `j' == 3{ 
		ge emplyd_es = 0 
		replace emplyd_es = 100 if emplyd == 100 & (es7606_hov_br_nr == 1071) // Bakeries
		ge calc_hrly_wage_es = .
		replace calc_hrly_wage_es = calc_hrly_wage if (es7606_hov_br_nr == 1071)
		local sector Bakeries
		local z 14
	}
	
	foreach var in emplyd_es calc_hrly_wage_es{
	** Baseline regression 5 degree age polynomial including dummy at event time 0
	reg `var' tline_pos tline_pos_sq tline_pos_cu tline_pos_4th tline_pos_5th Dtime0 DtimeZ, vce(cl cohmxmonth)
	putexcel set "$out\Apndx_Table4_excl_apprentices_reg_estimats_sector_dmethd_dofile_reg02.xls", sheet(Estimates) modify
		#d; 
		putexcel A2=("Baseline (dummy e-time[0;0])");  putexcel E2=("Obs.");  
		putexcel B2=("Beta"); putexcel C2=("[95% Conf. Interval]");
		putexcel G2 =("Delta"); putexcel H2=("[95% Conf. Interval]"); 
		putexcel A10=("Employed [-24:+24]"); 
		
		local x = `x' + 1; putexcel E`x' =(e(N));
		matrix b=[_b[Dtime0], _b[Dtime0] - 1.96*_se[Dtime0], _b[Dtime0] + 1.96*_se[Dtime0]];
		putexcel B`x'=matrix(b);
		putexcel A`x' =("`sector': `var'"); 
		#d cr

	nlcom delta: _b[Dtime0] / (_b[_cons]+_b[tline_pos]*25+_b[tline_pos_sq]*25^2+_b[tline_pos_cu]*25^3+_b[tline_pos_4th]*25^4 +_b[tline_pos_5th]*25^5+_b[Dtime0]*0.5), post
	putexcel set "$out\Apndx_Table4_excl_apprentices_reg_estimats_sector_dmethd_dofile_reg02.xls", sheet(Estimates) modify
		#d; 
		matrix b=[_b[delta], _b[delta] - 1.96*_se[delta], _b[delta] + 1.96*_se[delta]];
		putexcel G`x'=matrix(b);
		#d cr
		
	count if emplyd_es == 100
	putexcel set "$out\Apndx_Table4_excl_apprentices_reg_estimats_sector_dmethd_dofile_reg02.xls", sheet(Estimates) modify
		#d; 	
		putexcel A`z'=("`sector'");
		putexcel F11=("Sector Total");
		putexcel F`z'=(`r(N)');
		#d cr
		
	** Retrieving some sector employment counts	
	count if emplyd_es == 100 & inrange(tline, -24, -13)	// 16 YRS
	putexcel B`z'=(`r(N)')
	count if emplyd_es == 100 & inrange(tline, -12, -1) 	// 17 YRS
	putexcel C`z'=(`r(N)')
	count if emplyd_es == 100 & inrange(tline, 1, 12) 		// 18 YRS
	putexcel D`z'=(`r(N)')
	count if emplyd_es == 100 & inrange(tline, 13, 24) 		// 19 YRS
	putexcel E`z'=(`r(N)')
		
	}
restore

** Retrieving some total employment counts
	count if emplyd == 100 // Everyone
	putexcel set "$out\Apndx_Table4_excl_apprentices_reg_estimats_sector_dmethd_dofile_reg02.xls", sheet(Estimates) modify
		#d; 	
		putexcel A16=("Total");
		putexcel F16=(`r(N)');
		#d cr	
		
	count if emplyd == 100 & inrange(tline, -24, -13) // Age 16
	putexcel set "$out\Apndx_Table4_excl_apprentices_reg_estimats_sector_dmethd_dofile_reg02.xls", sheet(Estimates) modify
		#d; 	
		putexcel B11=("16 yrs");
		putexcel B16=(`r(N)');
		#d cr	
		
	count if emplyd == 100 & inrange(tline, -12, -1) // Age 17
	putexcel set "$out\Apndx_Table4_excl_apprentices_reg_estimats_sector_dmethd_dofile_reg02.xls", sheet(Estimates) modify
		#d; 	
		putexcel C11=("17 yrs");
		putexcel C16=(`r(N)');
		#d cr	
		
	count if emplyd == 100 & inrange(tline, 1, 12) // Age 18
	putexcel set "$out\Apndx_Table4_excl_apprentices_reg_estimats_sector_dmethd_dofile_reg02.xls", sheet(Estimates) modify
		#d; 	
		putexcel D11=("18 yrs");
		putexcel D16=(`r(N)');
		#d cr	
		
	count if emplyd == 100 & inrange(tline, 13, 24) // Age 19
	putexcel set "$out\Apndx_Table4_excl_apprentices_reg_estimats_sector_dmethd_dofile_reg02.xls", sheet(Estimates) modify
		#d; 	
		putexcel E11=("19 yrs");
		putexcel E16=(`r(N)');
		#d cr	

}

